package listbyorder.access001_100.test67;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/3 20:00
 */
public class Solution1 {

    // 二进制求和
    public String addBinary(String a, String b) {
        if (a.equals("")) return b;
        if (b.equals("")) return a;
        StringBuilder ans = new StringBuilder();
        int lenA = a.length() - 1;
        int lenB = b.length() - 1;
        int carry = 0;
        while (lenA >= 0 || lenB >= 0) {
            int c1 = lenA >= 0 ? a.charAt(lenA) - '0' : 0;
            int c2 = lenB >= 0 ? b.charAt(lenB) - '0' : 0;
            int sum = c1 + c2 + carry;
            ans.append(sum % 2);
            carry = sum / 2;
            lenA--;
            lenB--;
        }
        if (carry != 0) {
            ans.append(1);
        }
        return ans.reverse().toString();
    }
}
